const seq = require("../db/seq");
const { QueryTypes,Op } = require("sequelize");
const moment = require("moment")
const Category = require("../model/category.model")
class CateService {
  // 查询列表
  async selectAll(params) {
    console.log(params,"params")
    const {page,size} = params;
    let offset = (page-1)*size;
    let total;
    if (params && params.typeName) {
      const { typeName } = params;
      total = await Category.count({
        where:{
          typeName:{
            [Op.like]:`%${typeName}%`,
          }
        }
      })
      const cate = await seq.query(
        "select * from category where typeName like :typeName limit :limit offset :offset",
        {
          replacements: { typeName: `%${typeName}%`,offset,limit:size },
          type: QueryTypes.SELECT,
        }
      );
      return {data:cate,total,pageSize:size,currentPage:page};
    } else {
      total = await Category.count();

      const cate = await seq.query("select * from category limit :limit offset :offset", {
        replacements: { offset,limit:size },
        type: QueryTypes.SELECT,
      });
      return {data:cate,total,pageSize:size,currentPage:page};;
    }
  }
  // 按id查询
  async selectById(id) {
    const res = await Category.findOne({ where: { id: id } });
    return res;
  }

  // 修改cate信息
  async modifyService(params) {
    console.log(params, "catepara");
    const { typeName, icon, id } = params;
    if (typeName && icon) {
      const res = seq.query(
        "update category set typeName = :typeName, icon = :icon where id = :id",
        {
          replacements: { typeName, icon, id },
          type: QueryTypes.UPDATE,
        }
      );
      return res;
    } else {
      if (!icon) {
        const res = seq.query(
          "update category set typeName = :typeName  where id = :id",
          {
            replacements: { typeName, id },
            type: QueryTypes.UPDATE,
          }
        );
        return res;
      }
    }
  }

  // 添加cate
  async addService(params) {
    const time = moment().valueOf();
    console.log(params, "cateadd");
    const { typeName, icon } = params;
    if (typeName && icon) {
      const res = seq.query(
        "insert into category (typeName,icon,createTime) values(:typeName,:icon,:time)",
        {
          replacements: { typeName, icon,time },
          type: QueryTypes.INSERT,
        }
      );
      console.log("addssssssssssss",res);

      return res;
    } else {
      if (!icon) {
        const res = await seq.query(
          "insert into category (typeName,createTime) values(:typeName,:time)",
          {
            replacements: { typeName,time },
            type: QueryTypes.INSERT,
          }
        );
      console.log("addssssssssssss",res);

        return res;
      }
    }
  }

  // 按id查询信息
  async queryInfoService(id) {
    const res = await seq.query("select * from category where id = ?", {
      replacements: [id],
      type: QueryTypes.SELECT,
    });
    console.log("service", res);
    if (res && res[0]) {
      return res[0];
    }
  }

  // 按id删除
  async deleteService(id) {
     const res = await seq.query("delete from category where id = ?", {
      replacements: [id],
      type: QueryTypes.DELETE,
    });
    
  }
 
  // 按ids删除（选中）
  async deleteSelectService(ids) {
    // 动态生成id，直接传数组
    const res =await seq.query("delete from category where id in (?)", {
      replacements: [ids],
      type: QueryTypes.DELETE,
    });
    console.log(res,"res2")
  }
}

module.exports = new CateService();
